﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace Demo.SharePoint.Utilities
{
    public class GoogleMaps
    {
        private static SPWeb _topWeb;
        private static SPSite _site;
        internal const string Pname = "stm_googlemapskey";

        public GoogleMaps(SPSite site, string apiKey)
        {
            _site = site;
            ApiKey = apiKey;
        }

        //internal static void SetApiKey(SPSite site, string apiKey)
        //{
        //    _site = site;
        //    ApiKey = apiKey;
        //}

        //internal static string GetApiKey(SPSite site)
        //{
        //    _site = site;
        //    return ApiKey;
        //}

        internal static void SetApiKey(SPWeb rWeb, string apiKey)
        {
            rWeb = rWeb.Site.RootWeb;
            SPPropertyBag props = rWeb.Properties;
            try
            {
                if (!props.ContainsKey(Pname))
                {
                    props.Add(Pname, apiKey);
                }
                else
                {
                    props[Pname] = apiKey;
                }
                props.Update();
            }
            catch
            {
                if (!rWeb.Properties.ContainsKey(Pname))
                {
                    rWeb.Properties.Add(Pname, apiKey);
                }
                else
                {
                    rWeb.Properties[Pname] = apiKey;
                }
                try
                {
                    rWeb.Properties.Update();
                }
                catch { }
            }
        }

        /// <summary>
        /// Passing in any SPWeb object will suffice. The <paramref name="rWeb"/> object
        /// is used to get the Site Collection's RootWeb property.
        /// </summary>
        /// <param name="rWeb"></param>
        /// <returns></returns>
        internal static string GetApiKey(SPWeb rWeb)
        {
            string retVal = string.Empty;
            rWeb = rWeb.Site.RootWeb;
            if (rWeb.Properties.ContainsKey(Pname))
            {
                retVal = rWeb.Properties[Pname];
            }
            return retVal;
        }

        internal string ApiKey
        {
            get
            {
                string retVal = string.Empty;
                if (TopWeb.Properties.ContainsKey(Pname))
                {
                    retVal = TopWeb.Properties[Pname];
                }
                return retVal;
            }
            set
            {
                SetApiKey(TopWeb, value);
            }
        }

        internal SPWeb TopWeb
        {
            get
            {
                if (_site == null)
                {
                    _site = SPContext.Current.Site;
                }
                if (_topWeb == null)
                {
                    _topWeb = _site.RootWeb;
                }
                return _topWeb;
            }
        }
    }
}
